import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:voice_flip_clock/theme/app_theme.dart';
import 'package:package_info_plus/package_info_plus.dart'; // 添加这个导入

class SettingsPage extends StatefulWidget {
  const SettingsPage({super.key});

  @override
  _SettingsPageState createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  String _selectedOption = 'none'; // 默认不播报
  final GetStorage box = GetStorage();
  String _appVersion = ''; // 用于存储应用版本号

  @override
  void initState() {
    super.initState();
    // 从本地存储中读取用户的选择
    _selectedOption = box.read('selectedOption') ?? 'none';
    _getAppVersion(); // 获取应用版本号
  }

  Future<void> _getAppVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      _appVersion = packageInfo.version; // 获取版本号
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('设置'),
        backgroundColor: AppTheme.backgroundColor,
      ),
      body: SingleChildScrollView( // 添加可滚动的视图
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '语音播报设置',
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 20),
            ListTile(
              title: const Text('每秒播报'),
              leading: Radio<String>(
                value: 'second',
                groupValue: _selectedOption,
                onChanged: (value) {
                  setState(() {
                    _selectedOption = value!;
                    box.write('selectedOption', _selectedOption); // 存储选择
                  });
                },
              ),
            ),
            ListTile(
              title: const Text('每分钟播报'),
              leading: Radio<String>(
                value: 'minute',
                groupValue: _selectedOption,
                onChanged: (value) {
                  setState(() {
                    _selectedOption = value!;
                    box.write('selectedOption', _selectedOption); // 存储选择
                  });
                },
              ),
            ),
            ListTile(
              title: const Text('每小时播报'),
              leading: Radio<String>(
                value: 'hour',
                groupValue: _selectedOption,
                onChanged: (value) {
                  setState(() {
                    _selectedOption = value!;
                    box.write('selectedOption', _selectedOption); // 存储选择
                  });
                },
              ),
            ),
            ListTile(
              title: const Text('不播报'),
              leading: Radio<String>(
                value: 'none',
                groupValue: _selectedOption,
                onChanged: (value) {
                  setState(() {
                    _selectedOption = value!;
                    box.write('selectedOption', _selectedOption); // 存储选择
                  });
                },
              ),
            ),
            const SizedBox(height: 20),
            Text(
              '当前版本: $_appVersion', // 显示当前版本号
              style: const TextStyle(fontSize: 16),
            ),
          ],
        ),
      ),
    );
  }
}